package com.giantlizard.cloud.system.controller;

import com.giantlizard.cloud.base.generics.R;
import com.giantlizard.cloud.system.dto.SysOrgDto;
import com.giantlizard.cloud.system.entity.SysOrg;
import com.giantlizard.cloud.system.service.SysOrgService;
import com.giantlizard.cloud.system.vo.SysOrgVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统-机构 控制器
 */
@RestController
@RequestMapping("/org")
@Api(value = "系统-机构", tags = {"系统-机构"})
public class SysOrgController {

    private final SysOrgService sysOrgService;

    public SysOrgController(SysOrgService sysOrgService) {
        this.sysOrgService = sysOrgService;
    }

    @ApiOperation(value = "获取所有机构")
    @GetMapping("/list")
    public R<List<SysOrg>> list() {
        return R.ok(sysOrgService.list());
    }

    @ApiOperation(value = "获取所有机构树结构")
    @GetMapping("/tree")
    public R<List<SysOrgVo>> tree() {
        return R.ok(sysOrgService.tree());
    }

    @ApiOperation(value = "根据ID获取机构")
    @GetMapping("/{id}")
    public R<SysOrg> getById(@ApiParam(name = "id", required = true, value = "机构ID") @PathVariable String id) {
        return R.ok(sysOrgService.getById(id));
    }

    @ApiOperation(value = "新增机构")
    @PostMapping
    public R<Boolean> add(@RequestBody SysOrgDto dto) {
        sysOrgService.saveOrg(dto);
        return R.ok();
    }

    @ApiOperation(value = "更新机构")
    @PutMapping
    public R<Boolean> update(@RequestBody SysOrg sysOrg) {
        return R.ok(sysOrgService.updateById(sysOrg));
    }

    @ApiOperation(value = "删除机构")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@ApiParam(name = "id", required = true, value = "机构ID") @PathVariable String id) {
        return R.ok(sysOrgService.removeById(id));
    }
} 